package com.hspedu.File;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

/**
 * @Author Stringzhua
 * @Date 2024/3/16 12:22
 * description:演示创建文件
 */
public class FileCreate {
    public static void main(String[] args) {

    }
    //方式1:new File(String pathname)
    //根据路径构建一个file对象
    @Test
    public void create01() {
        String filePath = "e:\\qiyeke\\filecontroller\\news1.txt";
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式2:new File(File parent,String child)
    // 根据父目录文件加子路径构建
    //e:\\news2.txt
    @Test
    public void create02() {
        File parentfile = new File("e:\\qiyeke\\filecontroller\\");
        String fileName = "news2.txt";
        //这里的file对象,在java程序中,只是一个java对象
        //只有执行createNewFile方法,才会真正的,在磁盘创建文件
        File file = new File(parentfile, fileName);

        try {
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    //方式3:new File(String parent,String child)
    public void create03(){
        String parentPath="e:\\qiyeke\\filecontroller\\";
        String fileName="news4.txt";
        File file = new File(parentPath, fileName);
        try {
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}